import * as echarts from '../../../ec-canvas/echarts';
import HttpMethod from "../../constants/http-method-constant";
import HttpRequestService from "../../services/http-request-service";
import DynamicElementDefinition from "../dynamic-element-definition";
import ChartTypeConstant from "./chart-type-constant";
import EChartElementOptionFactory from "./echart-element-option-factory";

Component({
  properties: {
    definition: Object,   // DynamicElementDefinition
  },
  data: {
    option: {},
  },
  observers: {
    'definition': function () {
      this._getChartOption();
    },
  },
  methods: {
    _getChartOption: async function (): Promise<void> {
      const definition: any = this.data.definition;

      if (definition) {
        // Generates echart option.
        const option = await EChartElementOptionFactory.generateOptions(definition as DynamicElementDefinition);

        // Registers data for map chart.
        if (definition.config.optionCfg.type === ChartTypeConstant.map) {
          const mapJson = (await HttpRequestService.makeOriginalRequest(HttpMethod.get, 'https://www.wenlu.net.cn/sf-test/smart-farm-resources/dynamic-page/china-map.min.json')).payload;

          echarts.registerMap('CN', mapJson);
        }

        this.setData({
          option: option,
        });
      }
    },
  },
});